﻿#include "BrowserTableHandlerLoadPointForcesMoments.h"
#include "OperatorsInterface/OperAbstractLoad.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadExcitationForceMoments.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QCheckBox>

namespace GUI
{
    void BrowserTableHandlerLoadPointForcesMoments::updateTable()
    {
        if (_tableWidget == nullptr ) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Enabled") << tr("Connection")
            << tr("Force") << tr("Moment") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerLoadPointForcesMoments::updateData()
    {
        if (_tableWidget == nullptr) return;
        if ( _loadMgr == nullptr)return;

        QList<Acoustics::FITKAcousticsAbstractLoad*> list = _loadMgr->getLoadByType(
            Acoustics::FITKAcousticsAbstractLoad::ExcitationForceMoments);

        //载荷数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换力和力矩
            Acoustics::FITKLoadExcitationForceMoments* pfm = dynamic_cast<Acoustics::FITKLoadExcitationForceMoments*>(list.at(i));
            if (pfm == nullptr) continue;

            //数据ID
            const int id = pfm->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(pfm->getDataObjectName(), id));
            QCheckBox* box = new QCheckBox("");
            box->setChecked(pfm->getEnabledState());
            connect(box, &QCheckBox::stateChanged, this, [pfm, id](int state) {
                if (state == Qt::Checked)  pfm->setEnabledState(true);
                else if (state == Qt::Unchecked)  pfm->setEnabledState(false);
                EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
                if (operGraph)operGraph->updateGraph(id);
            });
            _tableWidget->setCellWidget(i, 1, box);
            _tableWidget->setItem(i, 2, new QTableWidgetItem(pfm->getConnectionObjName(), id));

            QString forceMoment="None";
            if (pfm->getForceSpectraObj()) forceMoment = pfm->getForceSpectraObj()->getDataObjectName();
            _tableWidget->setItem(i, 3, new QTableWidgetItem(forceMoment, id));
            forceMoment = "None";
            if (pfm->getMomentSpectraObj()) forceMoment = pfm->getMomentSpectraObj()->getDataObjectName();
            _tableWidget->setItem(i, 4, new QTableWidgetItem(forceMoment, id));

            _tableWidget->setItem(i, 5, new QTableWidgetItem(pfm->getNotes(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractLoad * BrowserTableHandlerLoadPointForcesMoments::getLoadOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractLoad>("OperLoadPointForcesMoments");
    }

    bool BrowserTableHandlerLoadPointForcesMoments::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //Force和Moment能批量
        if (colIndex < 3|| colIndex > 4) return false;

        return true;
    }
}